# Copyright 2020 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:build_defs.oss.bzl", "iree_runtime_cc_library", "iree_runtime_cc_test")
load("//build_tools/bazel:cc_binary_benchmark.bzl", "cc_binary_benchmark")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

#===------------------------------------------------------------------------===#
# Public API
#===------------------------------------------------------------------------===#

iree_runtime_cc_library(
    name = "vm",
    hdrs = [
        "api.h",
    ],
    deps = [
        ":impl",
        "//runtime/src/iree/base",
    ],
)

# TODO(benvanik): make these srcs and only expose an api_cc.h.
iree_runtime_cc_library(
    name = "cc",
    hdrs = [
        "native_module_cc.h",
        "native_module_packing.h",
    ],
    deps = [
        ":vm",
        "//runtime/src/iree/base",
        "//runtime/src/iree/base/internal:span",
    ],
)

#===------------------------------------------------------------------------===#
# Implementation
#===------------------------------------------------------------------------===#

iree_runtime_cc_library(
    name = "impl",
    srcs = [
        "buffer.c",
        "context.c",
        "instance.c",
        "invocation.c",
        "list.c",
        "module.c",
        "native_module.c",
        "ref.c",
        "ref_cc.h",
        "shims.c",
        "stack.c",
    ],
    hdrs = [
        "buffer.h",
        "context.h",
        "instance.h",
        "invocation.h",
        "list.h",
        "module.h",
        "native_module.h",
        "ref.h",
        "shims.h",
        "stack.h",
        "type_def.h",
        "value.h",
        "variant.h",
    ],
    deps = [
        "//runtime/src/iree/base",
        "//runtime/src/iree/base/internal",
        "//runtime/src/iree/base/internal:synchronization",
    ],
)

iree_runtime_cc_test(
    name = "buffer_test",
    srcs = ["buffer_test.cc"],
    deps = [
        ":cc",
        ":impl",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "list_test",
    srcs = ["list_test.cc"],
    deps = [
        ":cc",
        ":impl",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "module_test",
    srcs = ["module_test.cc"],
    deps = [
        ":cc",
        ":impl",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "native_module_test",
    srcs = ["native_module_test.cc"],
    deps = [
        ":cc",
        ":impl",
        ":native_module_test_hdrs",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_library(
    name = "native_module_test_hdrs",
    hdrs = [
        "native_module_test.h",
    ],
    deps = [
        ":impl",
        "//runtime/src/iree/base",
    ],
)

cc_binary_benchmark(
    name = "native_module_benchmark",
    srcs = ["native_module_benchmark.cc"],
    deps = [
        ":impl",
        ":native_module_test_hdrs",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:benchmark",
        "//runtime/src/iree/testing:benchmark_main",
    ],
)

iree_runtime_cc_test(
    name = "ref_test",
    srcs = ["ref_test.cc"],
    deps = [
        ":cc",
        ":impl",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "stack_test",
    srcs = ["stack_test.cc"],
    deps = [
        ":impl",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

#===------------------------------------------------------------------------===#
# Common VM op implementations
#===------------------------------------------------------------------------===#

iree_runtime_cc_library(
    name = "ops",
    hdrs = [
        "ops.h",
    ],
    deps = [
        "//runtime/src/iree/base",
        "//runtime/src/iree/base/internal",
    ],
)

iree_runtime_cc_library(
    name = "ops_emitc",
    hdrs = [
        "ops_emitc.h",
    ],
)

iree_runtime_cc_library(
    name = "shims_emitc",
    hdrs = [
        "shims_emitc.h",
    ],
    deps = [
        ":impl",
        "//runtime/src/iree/base:core_headers",
    ],
)

exports_files(["module_impl_emitc.c"])
